package marpg.network;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {
	private static class ClientThread extends Thread {

		DataInputStream is = null;
		PrintStream os = null;
		Socket clientSocket = null;
		ClientThread t[];

		public ClientThread(Socket clientSocket, ClientThread[] t) {
			this.clientSocket = clientSocket;
			this.t = t;
		}

		@SuppressWarnings("deprecation")
		public void run() {
			Object line = null;
			String loginMessage;

			try {
				is = new DataInputStream(clientSocket.getInputStream());
				os = new PrintStream(clientSocket.getOutputStream());

				loginMessage = is.readLine();
				System.out.println("*** A new agent entered. ***");
				System.out.println("Login message: " + loginMessage);

				while (true) {
					line = is.readLine();
					if (((String) line).startsWith("/quit")) {
						break;
					}

					for (int i = 0; i <= 9; i++) {
						if (t[i] != null && t[i] == this) {
							t[i].os.println(line);
							break;
						}
					}
				}

				System.out.println("*** An agent is leaving. ***");

				// Clean up:
				// Set to null the current thread variable such that other client could
				// be accepted by the server
				for (int i = 0; i < 1; i++) {
					if (t[i] == this) {
						t[i] = null;
					}
				}

				// close the output stream
				// close the input stream
				// close the socket
				is.close();
				os.close();
				clientSocket.close();

			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	// Declaration section:
	// declare a server socket and a client socket for the server
	// declare an input and an output stream
	static Socket clientSocket = null;
	static ServerSocket serverSocket = null;

	// This chat server can accept up to 10 clients' connections
	static ClientThread t[] = new ClientThread[10];

	public static void main(String args[]) {
		// The default port
		int port_number = 2022;

		if (args.length < 1) {
			System.out.println("Usage: java MultiThreadChatServer \n" + "Now using port number=" + port_number);
		} else {
			port_number = Integer.valueOf(args[0]).intValue();
		}

		// Initialization section:
		// Try to open a server socket on port port_number (default 2222)
		// Note that we can't choose a port less than 1023 if we are not
		// privileged users (root)

		try {
			serverSocket = new ServerSocket(port_number);
		} catch (IOException e) {
			System.out.println(e);
		}

		// Create a socket object from the ServerSocket to listen and accept
		// connections.
		// Open input and output streams for this socket will be created in
		// client's thread since every client is served by the server in
		// an individual thread
		while (true) {
			try {
				clientSocket = serverSocket.accept();
				for (int i = 0; i <= 9; i++) {
					if (t[i] == null) {
						(t[i] = new EchoServer.ClientThread(clientSocket, t)).start();
						break;
					}
				}
			} catch (IOException e) {
				System.out.println(e);
			}
		}
	}
}